﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.Threading;
public partial class administrator_uc_admin_a_products : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonBLL.RegisterCKFinder(CKEditorBody);
            LoadDataToDropDownListCategory(DropdownParentID, 0);
            //LoadDataToDropDownListBrands(DropDownBrands);            
            txtNewPrice.Text = "0";
            txtOldPrice.Text = "0";
        }
    }
    private void LoadDataToDropDownListCategory(DropDownList cboName, int CateID)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "---Chọn nhóm sản phẩm---";
        DataTable dtb = new CmsBLL().Category_GetTree(CateID);
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "_Title", "CateID");
        }
    }
    private void LoadDataToDropDownListBrands(DropDownList cboName)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "---Không chọn---";
        DataTable dtb = new CmsBLL().Brands_GetList("","");
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "BrandName", "BrandID");
        }
    }
    protected void lbtAddNewItem_Click(object sender, EventArgs e)
    {
        if (CheckRequiteValue().Length>0)
        {
            lblMessage.Text = CheckRequiteValue();
            return;
        }
        //if (int.Parse("0" + new CmsBLL().Products_CheckProductID(txtProductID.Text.Trim())) > 0)
        //{
        //    lblMessage.Text = "Mã sản phẩm:<b><u> " + txtProductID.Text + "</u></b> đã tồn tại! nhập vào mã sản phẩm mới";
        //    return;
        //}
        lblMessage.Text = "";
        try
        {            
            string Image = string.Empty;
            string GetValue = CommonBLL.UploadMultiImageToServer(FileUploadImageDaiDien, true, true);            
            if (!string.IsNullOrEmpty(GetValue))
            {
                if (GetValue.IndexOf("ERROR") == -1)
                    Image = GetValue;
            }
            string OtherImage = UploadImageRelated();
            string ProductId = System.Guid.NewGuid().ToString();
            new CmsBLL().Products_InsertItem(ProductId,int.Parse("0"+DropdownParentID.SelectedValue), 0 ,txtProductName.Text,
                txtDescription.Text,CKEditorBody.Text,Image,OtherImage,float.Parse("0"+txtOldPrice.Text),float.Parse("0"+txtNewPrice.Text),DateTime.Now,DateTime.Now,0,
                "",txtKhuyenMaiInfo.Text,chkSPKhuyenMai.Checked,chkSPBanChay.Checked,chkStatus.Checked, int.Parse("0" + rdActive.SelectedValue) , chkSPNew.Checked);
            lblMessage.Text = "Thêm mới thành công!";
            LoadDataToDropDownListCategory(DropdownParentID, 0);
            //LoadDataToDropDownListBrands(DropDownBrands);
            ResetForm();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERROR! " + ex.Message.ToString();
        }
    }
    private void ResetForm()
    {
        //txtProductID.Text = "";
        DropdownParentID.SelectedValue = "0";
        txtProductName.Text = "";
        txtDescription.Text = "";
        //CKEditorDescription.Text = "";
        CKEditorBody.Text = "";
        rdActive.SelectedValue = "1";
        DropdownParentID.SelectedValue = "0";
        //DropDownBrands.SelectedValue = "0";
        txtNewPrice.Text = "0";
        txtOldPrice.Text = "0";
        //CKEditorBaoHanhInfo.Text = "";
        //txtBaoHanh.Text = "";
        txtKhuyenMaiInfo.Text = "";
        chkSPKhuyenMai.Checked = false;
        chkSPBanChay.Checked = false;
        chkStatus.Checked = true;
        txtProductName.Focus();
        //txtProductID.Focus();
    }
    private string CheckRequiteValue()
    {
        string RetValue = string.Empty;
        //if (txtProductID.Text.Length < 3)
        //    RetValue += "Mã sản phẩm >=3 kí tự |";
        //if (txtProductID.Text.Contains("/"))
        //    RetValue += "Mã sản phẩm không được chứa kí tự '/' |";
        if (txtProductName.Text == "")
            RetValue += " | Chưa nhập tên sản phẩm |";
        if (DropdownParentID.SelectedValue == "0")
            RetValue += " | Chưa chọn nhóm sản phẩm";
        return RetValue;
    }
    private string UploadImageRelated()
    {
        string strImageRelated = string.Empty;
        try
        {
            // Get the HttpFileCollection
            //HttpFileCollection hfc = Request.Files;
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0 && hfc.AllKeys[i].Contains("FileUploadImageRelated"))
                {
                    string FileName = hpf.FileName.Substring(hpf.FileName.LastIndexOf("\\") + 1);
                    string strFileName = CommonBLL.GetFilename(FileName);
                    hpf.SaveAs(Server.MapPath("" + DAL.EnvInfo.PathUpload + "images/") + "\\" +
                      System.IO.Path.GetFileName(strFileName));
                    CommonBLL.UploadImageToSmallImage(strFileName, hpf.InputStream, 170, 160);
                    if (string.IsNullOrEmpty(strImageRelated))
                        strImageRelated += strFileName;
                    else
                        strImageRelated += ";" + strFileName;
                }
            }
        }
        catch (Exception)
        {

        }
        return strImageRelated;
    }
}